var utils = require('../../../util/utils');
var time = require('../../../util/time');
var User = require('../../login/lib/userSchema');
var Commander = require('../../../domain/commanderMoney');
var xlsx = require('node-xlsx');
var fs = require('fs');
var AsyncLock = require('async-lock');

const {
    missionExists,
    initDailyMission,
    getMissionInfo,
    receiveInviteReward, //领取邀请好友奖励
    receiveSonVideoReward,
    receiveGrandSonVideoReward,
    receivePersonalVideoReward,
    achievementMissionAction,
    receiveAchievementRewardAction,
    getProfitAction,
    receiveMoneyVideoReward,
    test
} = require('../lib/action');

const {
    Msg,
    Code,
    LockKey
} = require('../lib/consts');

module.exports = function (app) {
    return new Handler(app);
};
const Handler = function (app) {
    this.app = app;
};

const handler = Handler.prototype;

var lock = new AsyncLock({timeout: 1000});

/**
 * 获取每日任务的完成信息
 * @param msg
 * @param session
 * @param next
 */
handler.c2sDailyMissionInfo = async function (msg, session, next) {
    let uid = session.get('uid');
    try {
        //任务是否存在
        let exists = await missionExists.call(this, {session, uid});
        if (!exists) {
            //任务不存在初始化任务
            await initDailyMission.call(this, {session, uid})
        }
        //获取任务信息
        let mission = await getMissionInfo.call(this, session, uid);
        next(null, {code: Code.SUC, data: mission});
    } catch (err) {
        console.error('c2sDailyMissionInfo err:' + err);
        next(null, {code: Code.ERR, msg: err.toString()});
    }
}

/**
 * 获取分红信息
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sProfitInfo = async function (msg, session, next) {
    let uid = session.get('uid');
    try {
        //获取分红信息
        let mission = await getProfitAction.call(this, session, uid);
        next(null, {code: Code.SUC, data: mission});
    } catch (err) {
        console.error('c2sProfitInfo err:' + err);
        next(null, {code: Code.ERR, msg: err.toString()});
    }
}

/**
 * 领取邀请好友获得的任务奖励
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sReceiveInviteReward = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    //获取任务锁
    lock.acquire(LockKey.WITHDRAW_LOCK + uid, async function () {
        try {
            //领取石油奖励
            await receiveInviteReward.call(self, {session, uid});
            next(null, {code: Code.SUC});
        } catch (err) {
            console.error('c2sReceiveInviteReward err 1:' + err);
            next(null, {code: Code.ERR, msg: err.toString()});
        }
    }, function (err, ret) {
        //锁释放
        if (!!err) {
            console.error('c2sReceiveInviteReward err 2:' + err)
        }
    })
}

/**
 * 领取徒弟观看视频得到的石油奖励
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sSonVideoReward = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    //获取任务锁
    lock.acquire(LockKey.DAILY_MISSION_LOCK + uid, async function () {
        try {
            //领取石油奖励
            await receiveSonVideoReward.call(self, {session, uid});
            next(null, {code: Code.SUC});
        } catch (err) {
            console.error('c2sSonVideoReward err 1:' + err);
            next(null, {code: Code.ERR, msg: err.toString()});
        }
    }, function (err, ret) {
        //锁释放
        if (!!err) {
            console.error('c2sSonVideoReward err 2:' + err)
        }
    })
}

/**
 * 领取徒孙观看视频得到的石油奖励
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sGrandSonVideoReward = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    //获取任务锁
    lock.acquire(LockKey.DAILY_MISSION_LOCK + uid, async function () {
        try {
            //领取石油奖励
            await receiveGrandSonVideoReward.call(self, {session, uid});
            next(null, {code: Code.SUC});
        } catch (err) {
            console.error('c2sGrandSonVideoReward err 1:' + err);
            next(null, {code: Code.ERR, msg: err.toString()});
        }
    }, function (err, ret) {
        //锁释放
        if (!!err) {
            console.error('c2sGrandSonVideoReward err 2:' + err)
        }
    })
}

/**
 * 领取用户自己观看视频得到的石油奖励
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sReceivePersonalVideoReward = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    //获取任务锁
    lock.acquire(LockKey.DAILY_MISSION_LOCK + uid, async function () {
        try {
            //领取石油奖励
            await receivePersonalVideoReward.call(self, {session, uid});
            next(null, {code: Code.SUC});
        } catch (err) {
            console.error('c2sReceivePersonalVideoReward err 1:' + err);
            next(null, {code: Code.ERR, msg: err.toString()});
        }
    }, function (err, ret) {
        //锁释放
        if (!!err) {
            console.error('c2sReceivePersonalVideoReward err 2:' + err)
        }
    })
}

/**
 * 获取成就任务信息
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sAchievementMission = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');

    try {
        let mission = await achievementMissionAction.call(self, {session, uid});
        next(null, {code: Code.SUC, msg: Msg.RESULT_SUCCESS, data: mission});
    } catch (err) {
        console.error('c2sAchievementMission err 1:' + err);

        next(null, {code: Code.ERR, msg: err.toString()});
    }
}

/**
 * 领取成就任务奖励
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sReceiveAchievementReward = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    let task = msg.task;
    try {
        await receiveAchievementRewardAction.call(self, {session, uid, task});
        next(null, {code: Code.SUC, msg: Msg.RESULT_SUCCESS});
    } catch (err) {
        console.error('c2sReceiveAchievementReward err 1:' + err);

        next(null, {code: Code.ERR, msg: err.toString()});
    }
}

/**
 * 领取每日0.3元奖励
 * @param msg
 * @param session
 * @param next
 * @returns {Promise<void>}
 */
handler.c2sReceiveMoneyVideoReward = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    //获取任务锁
    lock.acquire(LockKey.DAILY_MISSION_LOCK + uid, async function () {
        try {
            //领取每日0.3元奖励
            await receiveMoneyVideoReward.call(self, {session, uid});
            next(null, {code: Code.SUC});
        } catch (err) {
            console.error('c2sReceiveMoneyVideoReward err 1:' + err);
            next(null, {code: Code.ERR, msg: err.toString()});
        }
    }, function (err, ret) {
        //锁释放
        if (!!err) {
            console.error('c2sReceiveMoneyVideoReward err 2:' + err)
        }
    })
}

/**
 * 本地测试函数
 * @returns {Promise<void>}
 */
handler.c2sTest = async function (msg, session, next) {
    let self = this;
    let uid = session.get('uid');
    await test.call(self, {session, uid});
    next(null, {code: Code.SUC});
}
